﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FruitCake.SupportLibrary;
using Orchard.ContentManagement;
using Orchard.Environment.Extensions;
using Orchard.Localization;

namespace FruitCake.ContactPage.Models
{
    [OrchardFeature("FruitCake.ContactPage")]
    public class ContactPagePart : ContentPart<ContactPagePartRecord>
    {
        #region Form

        public bool IsNameVisible 
        {
            get { return FlagsHelper.IsSet(Record.Fields, FieldFlags.NameField); }
            set { Record.Fields = FlagsHelper.Change(Record.Fields, FieldFlags.NameField, value); }
        }

        public bool IsReasonVisible 
        {
            get { return FlagsHelper.IsSet(Record.Fields, FieldFlags.ReasonField); }
            set { Record.Fields = FlagsHelper.Change(Record.Fields, FieldFlags.ReasonField, value); }
        }

        public bool IsSubjectVisible
        {
            get { return FlagsHelper.IsSet(Record.Fields, FieldFlags.SubjectField); }
            set { Record.Fields = FlagsHelper.Change(Record.Fields, FieldFlags.SubjectField, value); }
        }

        public string NameCaption 
        {
            get { return Record.NameCaption; }
            set { Record.NameCaption = value; }
        }

        public string MailCaption 
        {
            get { return Record.MailCaption; }
            set { Record.MailCaption = value; }
        }

        public string ReasonCaption
        {
            get { return Record.ReasonCaption; }
            set { Record.ReasonCaption = value; }
        }

        public string ReasonsList 
        {
            get { return Record.ReasonsList; }
            set { Record.ReasonsList = value; }
        }

        public string SubjectCaption
        {
            get { return Record.SubjectCaption; }
            set { Record.SubjectCaption = value; }
        }

        public bool SubjectIsRequired
        {
            get { return Record.SubjectIsRequired; }
            set { Record.SubjectIsRequired = value; }
        }

        public string TextCaption 
        {
            get { return Record.TextCaption; }
            set { Record.TextCaption = value; }
        }

        public int TextMaxLength 
        {
            get { return Record.TextMaxLength; }
            set { Record.TextMaxLength = value; }
        }

        #endregion

        #region Layout

        public string TextClass
        {
            get { return Record.TextClass; }
            set { Record.TextClass = value; }
        }

        public int TextRows
        {
            get { return Record.TextRows; }
            set { Record.TextRows = value; }
        }

        public int TextCols
        {
            get { return Record.TextCols; }
            set { Record.TextCols = value; }
        }

        public string InputClass
        {
            get { return Record.InputClass; }
            set { Record.InputClass = value; }
        }

        public int InputSize
        {
            get { return Record.InputSize; }
            set { Record.InputSize = value; }
        }

        #endregion

        #region Mail

        public bool MailEnabled
        {
            get { return Record.MailEnabled; }
            set { Record.MailEnabled = value; }
        }

        public string ToList
        {
            get { return Record.ToList; }
            set { Record.ToList = value; }
        }

        public string CcList
        {
            get { return Record.CcList; }
            set { Record.CcList = value; }
        }

        public string BccList
        {
            get { return Record.BccList; }
            set { Record.BccList = value; }
        }

        public string SubjectTemplate
        {
            get { return Record.SubjectTemplate; }
            set { Record.SubjectTemplate = value; }
        }

        public string BodyTemplate
        {
            get { return Record.BodyTemplate; }
            set { Record.BodyTemplate = value; }
        }

        #endregion

        #region AfterSent

        public bool IsAfterSentOptionsVisible
        {
            get { return !string.IsNullOrWhiteSpace(RedirectTo) && !string.IsNullOrWhiteSpace(SentMessage); }
        }

        public string RedirectTo 
        {
            get { return Record.RedirectTo; }
            set { Record.RedirectTo = value; }
        }

        public string SentMessage 
        {
            get { return Record.SentMessage; }
            set { Record.SentMessage = value; }
        }

        #endregion

        #region Utilities

        public bool ValidateReasonRules()
        {
            if (!IsReasonVisible) return true;
            if (string.IsNullOrWhiteSpace(ReasonsList)) return true;
            return GetReasonRules() != null;
        }

        public IEnumerable<ReasonRule> GetReasonRules()
        {   
            try
            {
                if (string.IsNullOrWhiteSpace(ReasonsList)) return null;

                List<ReasonRule> list = new List<ReasonRule>();

                string[] lines = ReasonsList.Split(new char[] { '\n', '\f', '\r' },   StringSplitOptions.RemoveEmptyEntries);

                int index = 0;
                foreach (string line in lines)
                {
                    ReasonRule rule = ReasonRule.Create(index, line);
                    if (rule != null)
                    {
                        list.Add(rule);
                        index += 1;
                    }
                }

                return list;                    
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        #endregion
    }
}